#!/usr/bin/perl -w
##############################################################################
#
# test_Setup_B_Obj.pl
# ===================
#
# Author: Steven Liang <stliang@gmail.com>
#
# Purpose:
#   test_Setup_B_Obj shows that you can create a CiscoRouter object and put it in
#   a test setup just like real life.  This way, you can just grab a test setup
#   object and start using it just like real life.
#
# Steps:
#
#   1.) Instantiate a CiscoRouter object and call it C2600
#   2.) Start a telnet session to target host
#   3.) Log the entire telnet session to a file
#   4.) Login to the target host
#   5.) Execute Cisco 2600 router's "show ip route" command
#   6.) Parse the command output and extra out the default route info
#   7.) Release C2600's resource
#
# Expected result:
#
#   Telnet to cisco router and perform the show ip route command and exit cleanly.
#
##############################################################################

package main;
$VERSION = 1.01;

# If the PERLLIB environment variable is not set like
# "export PERLLIB=<lib directory>" then uncomment the
# following two lines:
# use FindBin qw($Bin); 
# use lib "$Bin/./lib";
use Setup_B;

my $mySetup = undef;

if ((defined $ENV{'PAT_ROOT'}) && (defined $ENV{'PAT_LOG'})) {
  # Instantiate a CiscoRouter object and call it C2600
  $mySetup = Setup_B->new();

  # Start a telnet session to target host
  $mySetup->{C2600}->openTelnet();

  # Log the entire telnet session to a file
  $mySetup->{C2600}->log($ENV{'PAT_LOG'} . "test_Setup_B_Obj.log");

  # Login to the target host
  $mySetup->{C2600}->login(undef, qr/Password: $/, qr/>/, qr/#/);

  # Execute Cisco 2600 router's "show ip route" command
  $myRoute = $mySetup->{C2600}->showIPRoute();

  #Parse the command output and extra out the default route info
  @lines = split ( /\n/, $myRoute);
  foreach $line (@lines) {
	if ($line =~ /S\*/) {
	    $line =~ m{.*(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}).* (\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})};
	    # $1 holds the first match and $2 holds the second match
	    print "\nDefault distination network: $1";
	    print "\nDefault gateway: $2\n";
	    last;
	}
  }
  # Release C2600's resource
  $mySetup->{C2600}->DESTROY();
}
undef $mySetup;
